#pragma once
#include <vector>
namespace qfw
{
	template<class T>
	class less
	{
	public:
		bool operator()(T x, T y)
		{
			return x > y;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(T x, T y)
		{
			return x < y;
		}
	};


	template <class T, class Container = vector<T>, class Comper = less<T>>
	class priority_queue
	{
	public:
		void adjust_up(int child)
		{
			int faster = (child - 1) / 2;
			while (child > 0)
			{
				if (_cmp(_con[child] , _con[faster]))
				{
					std::swap(_con[child], _con[faster]);
					child = faster;
					faster = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int faster)
		{
			int child = faster * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && _cmp(_con[child + 1] , _con[child]))
				{
					child++;
				}

				if (_cmp(_con[child],_con[faster]))
				{
					std::swap(_con[faster], _con[child]);
					faster = child;
					child = faster * 2 + 1; 
				}
				else
				{
					break;
				}
			}
		}

		priority_queue()
		{}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push(*first);
				++first;
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}

		void pop()
		{
			std::swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;
		Comper _cmp;
	};
}